
package com.zzvcom.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public class DateUtil
{
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd"; // 默认的日期格式

    public static final DateFormat[] ACCEPT_DATE_FORMATS = { // 定义可被转换成日期对象的字符串格式
    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
            new SimpleDateFormat("yyyyMMddHHmmss"),
            new SimpleDateFormat("yyyy-MM-dd"),
            new SimpleDateFormat("yyyyMMdd"), new SimpleDateFormat("yyyyMM"),
            new SimpleDateFormat(DEFAULT_DATE_FORMAT),
            new SimpleDateFormat("yyyy/MM/dd"),
            new SimpleDateFormat("yyyy-MM"), new SimpleDateFormat("yyyy")

    };

    public static void main(String[] agrs)
    {

        String dateStr = "19851023181212";
        System.out
                .println(format(stringToDate(dateStr), "yyyy-MM-dd HH:mm:ss"));

    }

    /**
     * 函数介绍：根据默认模式包日期对象转换成日期字符串 参数：date ,日期对象；parttern,日期字符格式 返回值：日期字符串
     */
    public static String format(Date date, String parttern)
    {
        DateFormat df = new SimpleDateFormat(parttern);
        return df.format(date);
    }

    /**
     * 函数介绍：根据默认模式包日期对象转换成日期字符串 参数：date ,日期对象 返回值：日期字符串
     */
    public static String format(Date date)
    {
        return format(date, DEFAULT_DATE_FORMAT);
    }

    /**
     * 函数介绍：根据日期字符串转换成日期对象 参数：strDate,日期字符串 返回值：date 对象
     */
    public static Date stringToDate(String strDate)
    {
        for (DateFormat df : ACCEPT_DATE_FORMATS)
        {
            try
            {
                return df.parse(strDate);
            }
            catch (Exception e)
            {
                continue;
            }
        }

        return null;
    }

    /**
     * 函数介绍：把页面上得到的日期字符转换成数据库需要数字 参数：datestr,日期字符串 返回值：数字字符
     */

    public static String StringToNum14(String datestr)
    {
        if (datestr == null || StringUtils.isBlank(datestr.trim()))
            return null;
        return format(stringToDate(datestr), "yyyyMMddHHmmss");
    }

    public static String StringToNum8(String datestr)
    {
        if (datestr == null || StringUtils.isBlank(datestr.trim()))
            return null;
        return format(stringToDate(datestr), "yyyyMMdd");
    }

    public static String StringToNum08(String datestr)
    {
        if (datestr == null || StringUtils.isBlank(datestr.trim()))
            return null;
        return format(stringToDate(datestr), "yyyyMMdd");
    }

    public static String StringToNum06(String datestr)
    {
        if (datestr == null || StringUtils.isBlank(datestr.trim()))
            return null;
        return format(stringToDate(datestr), "yyyyMM");
    }

    /**
     * 函数介绍：把日期对象转换成数据库需要数字 参数：日期对象 返回值：数字字符
     */
    public static String dateToNum14(Date date)
    {
        return format(date, "yyyyMMddHHmmss");
    }

    public static String StringHmsToNum6(String str)
    {
        return str.replaceAll(":", "");
    }

    /**
     * 得到应用服务器的系统时间.
     * 
     * @return yyyymmddhhmiss格式的当前时间
     * @author fanhuifeng
     */
    public static String getDateFormatss()
    {
        Calendar calendar = Calendar.getInstance();
        String date = calendar.get(Calendar.YEAR)
                + addZero("" + (calendar.get(Calendar.MONTH) + 1))
                + addZero("" + calendar.get(Calendar.DAY_OF_MONTH))
                + addZero("" + calendar.get(Calendar.HOUR_OF_DAY))
                + addZero("" + calendar.get(Calendar.MINUTE))
                + addZero("" + calendar.get(Calendar.SECOND));
        return date;
    }

    /**
     * 给不够2位的字符串前面加0.
     * 
     * @return 字符串，不够两位左补0
     * @author fanhuifeng
     */
    private static String addZero(String s)
    {
        if (s.length() < 2)
        {
            return "0" + s;
        }
        return s;
    }

    /***************************************************************************
     * 查询昨天的时间
     * 
     * @param day
     * @return
     */
    public static String getDateFormatYesterday(int day)
    {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, -day);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String nowtime = sdf.format(cal.getTime());
        return nowtime;
    }

    /**
     * 查询前一天的时间
     * 
     * @param day
     * @param num
     * @return
     */
    public static String getFormatPreDay(Calendar day, int num)
    {
        day.add(Calendar.DAY_OF_YEAR, -num);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String nowtime = sdf.format(day.getTime());
        return nowtime;
    }

    /**
     * 查询前一周的时间
     * 
     * @param day
     * @param num
     * @return
     */
    public static String getFormatPreWeek(Calendar day, int num)
    {
        day.add(Calendar.WEEK_OF_YEAR, -num);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String nowtime = sdf.format(day.getTime());
        return nowtime;
    }

    /**
     * 查询前一个月的时间
     * 
     * @param day
     * @param num
     * @return
     */
    public static String getFormatPreMonth(Calendar day, int num)
    {
        day.add(Calendar.MONTH, -num);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String nowtime = sdf.format(day.getTime());
        return nowtime;
    }

    /**
     * 查询前一年的时间
     * 
     * @param day
     * @param num
     * @return
     */
    public static String getFormatPreYear(Calendar day, int num)
    {
        day.add(Calendar.YEAR, -num);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String nowtime = sdf.format(day.getTime());
        return nowtime;
    }

}
